﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Database.Context;
using QuanLyThongTinCoSo.BaseForm;
using QuanLyThongTinCoSo.Biz;
using QuanLyThongTinCoSo.Common;
using QuanLyThongTinCoSo.Forms.Phong;
using Model = Database.Models;

namespace QuanLyThongTinCoSo.Forms.DonVi
{
    public partial class frmDoiDetail : frmDetail
    {
        private int doiId;

        public frmDoiDetail()
        {
            InitializeComponent();
            cboPhong.Focus();

            LoadPhong(null);

            Text = @"Thêm mới đội";
        }

        public frmDoiDetail(int id)
        {
            InitializeComponent();
            cboPhong.Focus();

            Text = @"Sửa thông tin đội";

            _fromtype = Constant.TypeForm.Edit;
            doiId = id;

            var doiBiz = new DoiBiz();
            var model = doiBiz.GetItemById(id) as Model.Doi;
            if (model == null) return;

            txtTenDoi.Text = model.TenDoi;
            txtDienGiai.Text = model.DienGiai;

            LoadPhong(model.Phong);
        }

        protected override bool Validation()
        {
            bool flag = true;

            errorProvider.Dispose();

            if (string.IsNullOrEmpty(txtTenDoi.Text))
            {
                errorProvider.SetError(txtTenDoi, "Bạn chưa nhập tên đội. Xin vui lòng kiểm tra lại");
                flag = false;
            }

            if (string.IsNullOrEmpty(Convert.ToString(cboPhong.Text)))
            {
                errorProvider.SetError(cboPhong, "Bạn chưa chọn phòng. Xin vui lòng kiểm tra lại");
                flag = false;
            }

            return flag;
        }

        protected override void Insert()
        {
            var context = new Context();
            var doiBiz = new DoiBiz(context);
            var phongBiz = new PhongBiz(context);

            var doi = new Model.Doi();

            doi.TenDoi = txtTenDoi.Text.Trim();
            doi.DienGiai = txtDienGiai.Text.Trim();

            var phongSelected = cboPhong.SelectedItem as ComboBoxItem;
            if (phongSelected != null)
            {
                int phongId = int.Parse(phongSelected.Value);
                var phong = phongBiz.GetItemById(phongId) as Model.Phong;
                if (phong != null) doi.Phong = phong;
            }

            doiBiz.Insert(doi);
        }

        protected override void Edit()
        {
            var context = new Context();
            var doiBiz = new DoiBiz(context);
            var phongBiz = new PhongBiz(context);
            var canBoBiz = new CanBoQuanLyBiz(context);

            var model = (Model.Doi) doiBiz.GetItemById(doiId);

            model.TenDoi = txtTenDoi.Text;
            model.DienGiai = txtDienGiai.Text.Trim();

            var phongSelected = cboPhong.SelectedItem as ComboBoxItem;
            if (phongSelected != null)
            {
                int phongId = int.Parse(phongSelected.Value);
                var phong = phongBiz.GetItemById(phongId) as Model.Phong;
                if (phong != null) model.Phong = phong;
            }

            doiBiz.Update(model);
        }

        private void LoadPhong(Model.Phong data)
        {
            cboPhong.Items.Clear();
            int selectedIndex = 0;
            int index = 0;

            var phongBiz = new PhongBiz();
            foreach (var phong in phongBiz.GetAll())
            {
                var newItem = new ComboBoxItem(phong.TenPhong, phong.Id.ToString());
                cboPhong.Items.Add(newItem);

                if (data != null && data.Id == phong.Id)
                {
                    selectedIndex = index;
                }

                index++;
            }

            if (cboPhong.Items.Count > 0)
            {
                cboPhong.SelectedIndex = selectedIndex;
            }
        }

        protected void AddNewPhong(object sender, EventArgs args)
        {
            var frm = Application.OpenForms["frmPhongList"] as frmPhongList;
            if (frm != null)
            {
                frm.WindowState = FormWindowState.Maximized;
                frm.BringToFront();
                frm.Activate();
            }
            else
            {
                frm = new frmPhongList {MdiParent = this, WindowState = FormWindowState.Maximized};
                frm.MdiParent = this;
                frm.Show();
            }
        }
    }
}